#ifndef _COMMON_HPP
#define _COMMON_HPP
#include "shader.hpp"
#include "texture.hpp"
#include <cstdio>
#include <cstdarg>
#include <cmath>

void 
logv(const char *fmt, ...) {
    const int len = 1024;
	  char buffer[len];
  	va_list args;
  	va_start(args, fmt);
  	vsnprintf(buffer, len, fmt, args);
  	va_end (args);
  	fprintf(stdout, "%s\n", buffer);
  	fflush(stdout);
}

// turn coordate <x, y> from screen pixel to ndc <nx, ny>. the origin of screen is left-up, the origin of ndc is the center.
void
turn_screencoord_to_ndc(int width, int height, int x, int y, float *nx, float *ny) {
	  // the y axis is reverted
	  *nx = (float)x/(float)width * 2 - 1;
	  *ny = -((float)y/(float)height * 2 - 1);
}

float 
math_fclampf(float v, float min, float max) {
    return fminf(max, fmaxf(v, min));
}

int 
math_iclampi(int v, int min, int max) {
    v = (v < min ? min : (v > max ? max : v));
    return v;
}

#endif
